home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GXDITHER < prev    next >
Text File  |  1991-10-26  |  10KB  |  289 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxdither.c */
  21. #include "gx.h"
  22. #include "gxfixed.h"
  23. #include "gxmatrix.h"
  24. #include "gzstate.h"
  25. #include "gzdevice.h"
  26. #include "gzcolor.h"
  27. #include "gzht.h"
  28.  
  29. /* 
  30.  *    Improved dithering for Ghostscript.  The underlying device imaging 
  31.  *    model supports dithering between two colors to generate intermediate
  32.  *    shades.  
  33.  *    
  34.  *    The strategy is to first see if the color is either pure white or
  35.  *    pure black.  In this case the problem is trivial.
  36.  *    
  37.  *     Next, if we are on a High Quality RGB display with 8 or more
  38.  *    bits for each R, G and B, we simply set the color and return.
  39.  *
  40.  *    Now, if the device is black and white, or the color happens
  41.  *    to be achromatic, we perform simple B/W dithering.
  42.  *    
  43.  *    Otherwise, things are a bit more complicated.  If the device 
  44.  *     supports N shades of each R, G and B independently, there are a total 
  45.  *    of N*N*N colors.  These colors form a 3-D grid in a cubical color 
  46.  *    space.  The following dithering technique works by locating the 
  47.  *    color we want in this 3-D color grid and finding the eight colors 
  48.  *     that surround it.  In the case of dithering into 8 colors with 1 
  49.  *    bit for each red, green and blue, these eight colors will always 
  50.  *    be the same.
  51.  *
  52.  *    Now we consider all possible diagonal paths between the eight colors
  53.  *    and chose the path that runs closest to our desired color in 3-D
  54.  *    color space.  There are 28 such paths.  Then we find the position
  55.  *    on the path that is closest to out color.
  56.  *
  57.  *    The search is made more fast by always reflecting our color into
  58.  *    the bottom octant of the cube and comparing it to 7 paths.
  59.  *    After the best path and the best position on that path are found,
  60.  *    the results are reflected back into the original color space.
  61.  *
  62.  *    NOTE: This code has been tested for B/W and Color imaging with
  63.  *    1, 2, 3 and 8 bits per component.
  64.  *
  65.  *    --- original code by Paul Haeberli @ Silicon Graphics - 1990
  66.  *    --- extensively revised by L. Peter Deutsch, Aladdin Enterprises
  67.  */
  68.  
  69. void gx_color_load(P2(gx_device_color *, gs_state *));
  70.  
  71. #define    WEIGHT1        (unsigned long)(100)    /* 1.0             */
  72. #define    WEIGHT2        (unsigned long)(71)    /* 1/sqrt(2.0)         */
  73. #define    WEIGHT3        (unsigned long)(62)    /* 1/sqrt(3.0)+tad     */
  74.  
  75. #define    DIAG_R        (0x1)
  76. #define    DIAG_G        (0x2)
  77. #define    DIAG_B        (0x4)
  78. #define    DIAG_RG        (0x3)
  79. #define    DIAG_GB        (0x6)
  80. #define    DIAG_BR        (0x5)
  81. #define    DIAG_RGB    (0x7)
  82.  
  83. static unsigned short lum[8] = {
  84.     (0*lum_blue_weight+0*lum_green_weight+0*lum_red_weight),
  85.     (0*lum_blue_weight+0*lum_green_weight+1*lum_red_weight),
  86.     (0*lum_blue_weight+1*lum_green_weight+0*lum_red_weight),
  87.     (0*lum_blue_weight+1*lum_green_weight+1*lum_red_weight),
  88.     (1*lum_blue_weight+0*lum_green_weight+0*lum_red_weight),
  89.     (1*lum_blue_weight+0*lum_green_weight+1*lum_red_weight),
  90.     (1*lum_blue_weight+1*lum_green_weight+0*lum_red_weight),
  91.     (1*lum_blue_weight+1*lum_green_weight+1*lum_red_weight),
  92. };
  93.  
  94. /* Note that this routine assumes that the incoming color */
  95. /* has already been mapped through the transfer functions. */
  96. void
  97. gx_color_render(gs_color *pcolor, gx_device_color *pdevc, gs_state *pgs)
  98. {    device *pdev = pgs->device;
  99.     unsigned long max_value, mulval;
  100. #define divval (max_color_param_long+1)
  101.     unsigned long hsize;
  102.     gx_device *dev;
  103.  
  104. /* Make a special check for black and white. */
  105.     if ( pcolor->is_gray )
  106.        {    if ( pcolor->luminance == 0 )
  107.            {    pdevc->color2 = pdevc->color1 = pdev->black;
  108.             pdevc->halftone_level = 0; /* pure color */
  109.             return;
  110.            }
  111.         else if ( pcolor->luminance == max_color_param )
  112.            {    pdevc->color2 = pdevc->color1 = pdev->white;
  113.             pdevc->halftone_level = 0; /* pure color */
  114.             return;
  115.            }
  116.        }
  117.  
  118. /* get a few handy values */
  119.     dev = pdev->info;
  120.     max_value = dev->max_rgb_value;
  121.     mulval = max_value+1;
  122.  
  123. /* If we are on a high quality RGB display, don't bother dithering. */
  124.     if (max_value >= 255)
  125.        {    color_param r = (pcolor->red*mulval)/divval;
  126.         color_param g = (pcolor->green*mulval)/divval;
  127.         color_param b = (pcolor->blue*mulval)/divval;
  128.         pdevc->color2 = pdevc->color1 =
  129.             (*dev->procs->map_rgb_color)(dev,r,g,b);
  130.         pdevc->halftone_level = 0;    /* pure color */
  131.         return;
  132.        }
  133.     hsize = (unsigned)pgs->halftone->order_size;
  134.  
  135. /* see if we should do it in black and white */
  136.     if ( !dev->has_color || pcolor->is_gray )
  137.        {
  138.  
  139. /* if bw device or gray color, use luminance of the color */
  140.         unsigned long nshades = hsize*max_value+1;
  141.         unsigned long lx = (nshades*color_luminance(pcolor))/divval;
  142.         color_param l = lx/hsize;
  143.         pdevc->halftone_level = lx%hsize;
  144.  
  145. /* check halftone level to see if we have to dither */
  146.         pdevc->color1 = (*dev->procs->map_rgb_color)(dev,l,l,l);
  147.         if ( pdevc->halftone_level == 0 )
  148.             pdevc->color2 = pdevc->color1;
  149.         else
  150.            {    ++l;
  151.             pdevc->color2 =
  152.                 (*dev->procs->map_rgb_color)(dev,l,l,l);
  153.             gx_color_load(pdevc, pgs);
  154.            }
  155.         return;
  156.        }
  157.  
  158. /* must do real color dithering */
  159.    {    unsigned long want_r = pcolor->red*max_value;
  160.     unsigned long want_g = pcolor->green*max_value;
  161.     unsigned long want_b = pcolor->blue*max_value;
  162.     color_param r = want_r/max_color_param_long;
  163.     color_param g = want_g/max_color_param_long;
  164.     color_param b = want_b/max_color_param_long;
  165.     /* rem_{r,g,b} are short, so we can get away with short arithmetic. */
  166.     color_param rem_r = (color_param)want_r-(r*max_color_param);
  167.     color_param rem_g = (color_param)want_g-(g*max_color_param);
  168.     color_param rem_b = (color_param)want_b-(b*max_color_param);
  169.     int adjust_r, adjust_b, adjust_g;
  170.     unsigned short amax;
  171.     unsigned long dmax;
  172.     int axisc, diagc;
  173.     unsigned short lum_invert;
  174.     unsigned long dot1, dot2, dot3;
  175.     int level;
  176.  
  177.     /* Check for no dithering required */
  178.     if ( !(rem_r | rem_g | rem_b) )
  179.        {    pdevc->color2 = pdevc->color1 =
  180.           (*dev->procs->map_rgb_color)(dev, r, g, b);
  181.         pdevc->halftone_level = 0;
  182.         return;
  183.        }
  184.  
  185. /* flip the remainder color into the 0, 0, 0 octant */
  186.     lum_invert = 0;
  187. #define half ((color_param)(max_color_param_long>>1))
  188.     if ( rem_r > half )
  189.         rem_r = max_color_param - rem_r,
  190.           adjust_r = -1, r++, lum_invert += lum_red_weight;
  191.     else
  192.         adjust_r = 1;
  193.     if ( rem_g > half)
  194.         rem_g = max_color_param - rem_g,
  195.           adjust_g = -1, g++, lum_invert += lum_green_weight;
  196.     else
  197.         adjust_g = 1;
  198.     if ( rem_b > half )
  199.         rem_b = max_color_param - rem_b,
  200.           adjust_b = -1, b++, lum_invert += lum_blue_weight;
  201.     else
  202.         adjust_b = 1;
  203.     pdevc->color1 = (*dev->procs->map_rgb_color)(dev, r, g, b);
  204. /* 
  205.  * Dot the color with each axis to find the best one of 7;
  206.  * find the color at the end of the axis chosen.
  207.  */
  208.     if ( rem_g > rem_r )
  209.        {    if ( rem_b > rem_g )
  210.             amax = rem_b, axisc = DIAG_B;
  211.         else
  212.             amax = rem_g, axisc = DIAG_G;
  213.         if ( rem_b > rem_r )
  214.             dmax = (unsigned long)rem_g+rem_b, diagc = DIAG_GB;
  215.         else
  216.             dmax = (unsigned long)rem_r+rem_g, diagc = DIAG_RG;
  217.        }
  218.     else
  219.        {    if ( rem_b > rem_r )
  220.             amax = rem_b, axisc = DIAG_B;
  221.         else
  222.             amax = rem_r, axisc = DIAG_R;
  223.         if ( rem_b > rem_g )
  224.             dmax = (unsigned long)rem_b+rem_r, diagc = DIAG_BR;
  225.         else
  226.             dmax = (unsigned long)rem_r+rem_g, diagc = DIAG_RG;
  227.        }
  228.  
  229.     dot1 = amax*WEIGHT1;
  230.     dot2 = dmax*WEIGHT2;
  231.     dot3 = (ulong)rem_r+rem_g+rem_b;    /* rgb axis */
  232.     if ( dot1 > dot2 )
  233.        {    if ( dot3*WEIGHT3 > dot1 )
  234.             diagc = DIAG_RGB,
  235.               level = (hsize * dot3) / (3 * max_color_param_long);
  236.         else
  237.             diagc = axisc,
  238.               level = (hsize * amax) / max_color_param_long;
  239.        }
  240.     else
  241.        {    if ( dot3*WEIGHT3 > dot2 )
  242.             diagc = DIAG_RGB,
  243.               level = (hsize * dot3) / (3 * max_color_param_long);
  244.         else
  245.             level = (hsize * dmax) / (2 * max_color_param_long);
  246.        };
  247. #ifdef DEBUG
  248. if ( gs_debug['c'] )
  249.    {    dprintf3("[c]rgb=%x,%x,%x -->\n",
  250.          (unsigned)pcolor->red, (unsigned)pcolor->green,
  251.          (unsigned)pcolor->blue);
  252.     dprintf6("   %x+%x,%x+%x,%x+%x;",
  253.         (unsigned)r, (unsigned)rem_r, (unsigned)g, (unsigned)rem_g,
  254.         (unsigned)b, (unsigned)rem_b);
  255.     dprintf3(" adjust=%d,%d,%d;\n",
  256.         adjust_r, adjust_g, adjust_b);
  257.    }
  258. #endif
  259.  
  260.     if ( (pdevc->halftone_level = level) == 0 )
  261.         pdevc->color2 = pdevc->color1;
  262.     else
  263.        {
  264. /* construct the second color, inverting back to original space if needed */
  265.         if (diagc & DIAG_R) r += adjust_r;
  266.         if (diagc & DIAG_G) g += adjust_g;
  267.         if (diagc & DIAG_B) b += adjust_b;
  268. /* get the second device color, sorting by luminance */
  269.         if ( lum[diagc] < lum_invert )
  270.            {    pdevc->color2 = pdevc->color1;
  271.             pdevc->color1 = (*dev->procs->map_rgb_color)(dev, r, g, b);
  272.             pdevc->halftone_level = level = hsize - level;
  273.            }
  274.         else
  275.             pdevc->color2 = (*dev->procs->map_rgb_color)(dev, r, g, b);
  276.         gx_color_load(pdevc, pgs);
  277.        }
  278.  
  279. #ifdef DEBUG
  280. if ( gs_debug['c'] )
  281.    {    dprintf5("[c]diagc=%d; color1=%lx, color2=%lx, level=%d/%d\n",
  282.          diagc, (ulong)pdevc->color1, (ulong)pdevc->color2,
  283.          level, (unsigned)hsize);
  284.    }
  285. #endif
  286.  
  287.    }
  288. }
  289.